select * from loja_saidas where ltrim(romaneio_produto) = '000099'
select TOP 100 * from loja_saidas_produto where romaneio_produto = '000099' and filial = '545_BAZAR 1'
select TOP 2 * FROM LOJA_ENTRADAS_PRODUTO

select TOP 2 * from loja_saidas_produto
select TOP 2 * FROM LOJA_ENTRADAS_PRODUTO


select TOP 2 * from loja_entradas where substring(filial_origem,1,3) = '525' order by romaneio_produto desc
select TOP 2* from loja_saidas where ltrim(romaneio_produto) = '000099'

select * from loja_saidas where substring(filial,1,3) = '525' order by romaneio_produto desc

SELECT * FROM FILIAIS WHERE FILIAL LIKE '525%'
SELECT * FROM FILIAIS WHERE FILIAL LIKE '546%'

BEGIN TRANSACTION
DECLARE
select * from series_nf where serie_nf = '1'


282393
282395
/*
update series_nf
set sequencial = sequencial + 1
where serie_nf = '1'
*/
select sum(A.QTDE_TOTAL) as qtde_total,sum(A.VALOR_TOTAL) valor_total
from loja_entradas a
where substring(a.filial,1,3) = '525'
and substring(a.filial_origem,1,3) in ('095','110','113','124','128','519','999')
and datepart(yyyy,a.emissao) = 2008
and ((substring(a.filial_origem,1,3) = '519' and cast(a.numero_nf_transferencia as int) in (00000896,00000897,00000898,00000899,00000900)) or
(substring(a.filial_origem,1,3) = '095' and cast(a.numero_nf_transferencia as int) in (001068,001069,001070,001073,001074,001075,001076,001077,001079,001080,001081,001082,001083,001084)) or 
(substring(a.filial_origem,1,3) = '110' and cast(a.numero_nf_transferencia as int) in (002047,002052,002061,002064,002065,002067,002068,002069,002070,002071,002073,002074,002078,002079,002081,002085,002086,002087)) or
(substring(a.filial_origem,1,3) = '113' and cast(a.numero_nf_transferencia as int) in (000964,000965,000663,000668,273730)) or  
(substring(a.filial_origem,1,3) = '124' and cast(a.numero_nf_transferencia as int) in (000741,000742,000743,000745,000746,000748,000750,000751,000752,000753,000756,000757,000760,000761,000765,000766,000767,000768,000769,000772,000773,000774,000775)) or   
(substring(a.filial_origem,1,3) = '128' and cast(a.numero_nf_transferencia as int) in (000711,000713)) or 
(substring(a.filial_origem,1,3) = '999' and rtrim(ltrim(a.numero_nf_transferencia)) in ('280218','280496','280508','280981','281647')))


select A.*ILIAL_ORIGEM AS ZS,A.FILIAL AS FF,B.FILIAL AS BB,A.CODIGO_TAB_PRECO,A.TIPO_ENTRADA_SAIDA,A.QTDE_TOTAL,A.VALOR_TOTAL,
B.PRODUTO,B.COR_PRODUTO ,B.EN1,B.EN2,B.EN3,B.EN4,B.EN5,B.EN6,B.EN7,B.EN8,B.EN9,B.EN10,B.EN11,B.EN12,B.EN13,B.EN14,
B.EN15,B.EN16,B.EN17,B.EN18,B.EN19,B.EN20,B.EN21,B.EN22,B.EN23,B.EN24,B.EN25,B.EN26,B.EN27,B.EN28,B.EN29,B.EN30,
B.EN31,B.EN32,B.EN33,B.EN34,B.EN35,B.EN36,B.EN37,B.EN38,B.EN39,B.EN40,B.EN41,B.EN42,B.EN43,B.EN44,B.EN45,B.EN46,
B.EN47,B.EN48,B.VALOR,B.PRECO4,B.PRECO3,B.PRECO2,B.PRECO1,B.QTDE_ENTRADA,B.DATA_PARA_TRANSFERENCIA
from loja_entradas a
join LOJA_ENTRADAS_PRODUTO b on (a.romaneio_produto = b.romaneio_produto and a.filial = b.filial)
join PRODUTO_CORES C on (B.produto = C.produto and B.cor_produto = C.cor_produto)
join PRODUTOS D on (C.produto = D.produto)
JOIN PRODUTOS_TAMANHOS E ON (D.GRADE = E.GRADE)
where substring(a.filial,1,3) = '525'
and substring(a.filial_origem,1,3) in ('095','110','113','124','128','519','999')
and datepart(yyyy,a.emissao) = 2008
and ((substring(a.filial_origem,1,3) = '519' and cast(a.numero_nf_transferencia as int) in (00000896,00000897,00000898,00000899,00000900)) or
(substring(a.filial_origem,1,3) = '095' and cast(a.numero_nf_transferencia as int) in (001068,001069,001070,001073,001074,001075,001076,001077,001079,001080,001081,001082,001083,001084)) or 
(substring(a.filial_origem,1,3) = '110' and cast(a.numero_nf_transferencia as int) in (002047,002052,002061,002064,002065,002067,002068,002069,002070,002071,002073,002074,002078,002079,002081,002085,002086,002087)) or
(substring(a.filial_origem,1,3) = '113' and cast(a.numero_nf_transferencia as int) in (000964,000965,000663,000668,273730)) or  
(substring(a.filial_origem,1,3) = '124' and cast(a.numero_nf_transferencia as int) in (000741,000742,000743,000745,000746,000748,000750,000751,000752,000753,000756,000757,000760,000761,000765,000766,000767,000768,000769,000772,000773,000774,000775)) or   
(substring(a.filial_origem,1,3) = '128' and cast(a.numero_nf_transferencia as int) in (000711,000713)) or 
(substring(a.filial_origem,1,3) = '999' and rtrim(ltrim(a.numero_nf_transferencia)) in ('280218','280496','280508','280981','281647')))
order by a.filial_origem,a.numero_nf_transferencia

select B.*
from loja_entradas a
join LOJA_ENTRADAS_PRODUTO b on (a.romaneio_produto = b.romaneio_produto and a.filial = b.filial)
join PRODUTO_CORES C on (B.produto = C.produto and B.cor_produto = C.cor_produto)
join PRODUTOS D on (C.produto = D.produto)
JOIN PRODUTOS_TAMANHOS E ON (D.GRADE = E.GRADE)
where substring(a.filial,1,3) = '525'
and substring(a.filial_origem,1,3) in ('095','110','113','124','128','519','999')
and datepart(yyyy,a.emissao) = 2008
and ((substring(a.filial_origem,1,3) = '519' and cast(a.numero_nf_transferencia as int) in (00000896,00000897,00000898,00000899,00000900)) or
(substring(a.filial_origem,1,3) = '095' and cast(a.numero_nf_transferencia as int) in (001068,001069,001070,001073,001074,001075,001076,001077,001079,001080,001081,001082,001083,001084)) or 
(substring(a.filial_origem,1,3) = '110' and cast(a.numero_nf_transferencia as int) in (002047,002052,002061,002064,002065,002067,002068,002069,002070,002071,002073,002074,002078,002079,002081,002085,002086,002087)) or
(substring(a.filial_origem,1,3) = '113' and cast(a.numero_nf_transferencia as int) in (000964,000965,000663,000668,273730)) or  
(substring(a.filial_origem,1,3) = '124' and cast(a.numero_nf_transferencia as int) in (000741,000742,000743,000745,000746,000748,000750,000751,000752,000753,000756,000757,000760,000761,000765,000766,000767,000768,000769,000772,000773,000774,000775)) or   
(substring(a.filial_origem,1,3) = '128' and cast(a.numero_nf_transferencia as int) in (000711,000713)) or 
(substring(a.filial_origem,1,3) = '999' and rtrim(ltrim(a.numero_nf_transferencia)) in ('280218','280496','280508','280981','281647')))
order by a.filial_origem,a.numero_nf_transferencia




select a.filial_origem as origem,a.filial as destino,a.numero_nf_transferencia as nota_fiscal,
CONVERT(varchar(30), a.emissao, 103) as emissao,b.produto as produto,b.preco1 as preco,b.qtde_entrada as qtd,
b.valor as valor,b.cor_produto as cor,C.DESC_COR_PRODUTO as nome_cor_produto,
cast(
CASE WHEN B.EN1 > 0 THEN cast(' ' +ltrim(cast(B.EN1 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_1)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN2 > 0 THEN cast(' ' +ltrim(cast(B.EN2 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_2)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN3 > 0 THEN cast(' ' +ltrim(cast(B.EN3 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_3)) + ')' as varchar(10))
ELSE '' END  +
CASE WHEN B.EN4 > 0 THEN cast(' ' +ltrim(cast(B.EN4 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_4)) + ')' as varchar(10))
ELSE '' END + 
CASE WHEN B.EN5 > 0 THEN cast(' ' +ltrim(cast(B.EN5 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_5)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN6 > 0 THEN cast(' ' +ltrim(cast(B.EN6 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_6)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN7 > 0 THEN cast(' ' +ltrim(cast(B.EN7 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_7)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN8 > 0 THEN cast(' ' +ltrim(cast(B.EN8 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_8)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN9 > 0 THEN cast(' ' +ltrim(cast(B.EN9 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_9)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN10 > 0 THEN cast(' ' +ltrim(cast(B.EN10 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_10)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN11 > 0 THEN cast(' ' +ltrim(cast(B.EN11 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_11)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN12 > 0 THEN cast(' ' +ltrim(cast(B.EN12 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_12)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN13 > 0 THEN cast(' ' +ltrim(cast(B.EN13 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_13)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN14 > 0 THEN cast(' ' +ltrim(cast(B.EN14 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_14)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN15 > 0 THEN cast(' ' +ltrim(cast(B.EN15 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_15)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN16 > 0 THEN cast(' ' +ltrim(cast(B.EN16 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_16)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN17 > 0 THEN cast(' ' +ltrim(cast(B.EN17 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_17)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN18 > 0 THEN cast(' ' +ltrim(cast(B.EN18 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_18)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN19 > 0 THEN cast(' ' +ltrim(cast(B.EN19 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_19)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN20 > 0 THEN cast(' ' +ltrim(cast(B.EN20 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_20)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN21 > 0 THEN cast(' ' +ltrim(cast(B.EN21 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_21)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN22 > 0 THEN cast(' ' +ltrim(cast(B.EN22 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_22)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN23 > 0 THEN cast(' ' +ltrim(cast(B.EN23 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_23)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN24 > 0 THEN cast(' ' +ltrim(cast(B.EN24 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_24)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN25 > 0 THEN cast(' ' +ltrim(cast(B.EN25 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_25)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN26 > 0 THEN cast(' ' +ltrim(cast(B.EN26 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_26)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN27 > 0 THEN cast(' ' +ltrim(cast(B.EN27 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_27)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN28 > 0 THEN cast(' ' +ltrim(cast(B.EN28 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_28)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN29 > 0 THEN cast(' ' +ltrim(cast(B.EN29 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_29)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN30 > 0 THEN cast(' ' +ltrim(cast(B.EN30 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_30)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN31 > 0 THEN cast(' ' +ltrim(cast(B.EN31 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_31)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN32 > 0 THEN cast(' ' +ltrim(cast(B.EN32 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_32)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN33 > 0 THEN cast(' ' +ltrim(cast(B.EN33 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_33)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN34 > 0 THEN cast(' ' +ltrim(cast(B.EN34 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_34)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN35 > 0 THEN cast(' ' +ltrim(cast(B.EN35 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_35)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN36 > 0 THEN cast(' ' +ltrim(cast(B.EN36 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_36)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN37 > 0 THEN cast(' ' +ltrim(cast(B.EN37 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_37)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN38 > 0 THEN cast(' ' +ltrim(cast(B.EN38 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_38)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN39 > 0 THEN cast(' ' +ltrim(cast(B.EN39 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_39)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN40 > 0 THEN cast(' ' +ltrim(cast(B.EN40 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_40)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN41 > 0 THEN cast(' ' +ltrim(cast(B.EN41 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_41)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN42 > 0 THEN cast(' ' +ltrim(cast(B.EN42 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_42)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN43 > 0 THEN cast(' ' +ltrim(cast(B.EN43 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_43)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN44 > 0 THEN cast(' ' +ltrim(cast(B.EN44 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_44)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN45 > 0 THEN cast(' ' +ltrim(cast(B.EN45 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_45)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN46 > 0 THEN cast(' ' +ltrim(cast(B.EN46 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_46)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN47 > 0 THEN cast(' ' +ltrim(cast(B.EN47 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_47)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN48 > 0 THEN cast(' ' +ltrim(cast(B.EN48 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_48)) + ')' as varchar(10))
ELSE '' END 
as varchar(400)) as qtd_tam
from loja_entradas a
join LOJA_ENTRADAS_PRODUTO b on (a.romaneio_produto = b.romaneio_produto and a.filial = b.filial)
join PRODUTO_CORES C on (B.produto = C.produto and B.cor_produto = C.cor_produto)
join PRODUTOS D on (C.produto = D.produto)
JOIN PRODUTOS_TAMANHOS E ON (D.GRADE = E.GRADE)
where substring(a.filial,1,3) = '525'
and substring(a.filial_origem,1,3) in ('095','110','113','124','128','519','999')
and datepart(yyyy,a.emissao) = 2008
and (
(substring(a.filial_origem,1,3) = '519' and cast(a.numero_nf_transferencia as int) in (00000896
,00000897
,00000898
,00000899
,00000900)) or
(substring(a.filial_origem,1,3) = '095' and cast(a.numero_nf_transferencia as int) in (001068  
,001069  
,001070  
,001073  
,001074  
,001075  
,001076  
,001077  
,001079  
,001080  
,001081  
,001082  
,001083  
,001084)) or 
(substring(a.filial_origem,1,3) = '110' and cast(a.numero_nf_transferencia as int) in (002047  
,002052  
,002061  
,002064  
,002065  
,002067  
,002068  
,002069  
,002070  
,002071  
,002073  
,002074  
,002078  
,002079  
,002081  
,002085  
,002086  
,002087)) or
(substring(a.filial_origem,1,3) = '113' and cast(a.numero_nf_transferencia as int) in (000964  
,000965  
,000663  
,000668  
,273730)) or  
(substring(a.filial_origem,1,3) = '124' and cast(a.numero_nf_transferencia as int) in (000741  
,000742  
,000743  
,000745  
,000746  
,000748  
,000750  
,000751  
,000752  
,000753  
,000756  
,000757  
,000760  
,000761  
,000765  
,000766  
,000767  
,000768  
,000769  
,000772  
,000773  
,000774  
,000775)) or   
(substring(a.filial_origem,1,3) = '128' and cast(a.numero_nf_transferencia as int) in (000711,000713)) or 
(substring(a.filial_origem,1,3) = '999' and rtrim(ltrim(a.numero_nf_transferencia)) in ('280218','280496','280508','280981','281647')))
order by a.filial_origem,a.numero_nf_transferencia

select top 10 * from loja_entradas
select top 10 * from loja_entradas

select cast((rand(30) * 10000000) as int)

